package repository

import (
	"gorm.io/gorm"
	"shop-bean/model"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
)

type IPickAddrRepository interface {
	Create(db *gorm.DB, addr *model.PickAddr) error
	Updates(db *gorm.DB, id uint, addr *model.PickAddr) error
	DeleteBatch(db *gorm.DB, ids []uint) error
	FindPage(db *gorm.DB, params *pagehelper.Params) ([]*model.PickAddr, error)
	Count(db *gorm.DB, params *pagehelper.Params) (int64, error)
}

type PickAddrRepository struct {
}

func NewPickAddrRepository() IPickAddrRepository {
	return &PickAddrRepository{}
}

func (r *PickAddrRepository) Updates(db *gorm.DB, id uint, addr *model.PickAddr) error {
	return db.Model(&model.PickAddr{}).Where("addr_id = ?", id).Updates(addr).Error
}

func (r *PickAddrRepository) Create(db *gorm.DB, addr *model.PickAddr) error {
	return db.Create(&addr).Error
}

func (r *PickAddrRepository) DeleteBatch(db *gorm.DB, ids []uint) error {
	return db.Delete(&model.PickAddr{}, ids).Error
}

func (r *PickAddrRepository) FindPage(db *gorm.DB, params *pagehelper.Params) (list []*model.PickAddr, err error) {
	tx := queryWrapper.ReuseWherePair(db.Model(&model.PickAddr{}), params.WherePair)
	return list, tx.Scopes(pagehelper.Paginate(params.Current, params.Size)).Find(&list).Error
}

func (r *PickAddrRepository) Count(db *gorm.DB, params *pagehelper.Params) (count int64, err error) {
	tx := queryWrapper.ReuseSelect(db.Model(&model.PickAddr{}), params.SelectFields)
	return count, queryWrapper.ReuseWherePair(tx, params.WherePair).Count(&count).Error
}
